Set Up

  • Install/load necessary R packages
  • Set working directory if necessary (or create a file path to use throughout the RMD to call the data from Box)

Read in water year data from CSVs in the intermediate_data directory

#read in necessary CSV files from prior RMDs
all_streamflow <- read_csv(here("intermediate_data", "all_streamflow.csv"))
air_temp_30_clean <- read_csv(here("intermediate_data", "air_temp_30_clean.csv"))
bogwell_clean <- read_csv(here("intermediate_data", "bogwell_clean.csv")) 
mc_clean <- read_csv(here("intermediate_data", "mc_clean.csv")) %>% 
  rename(datetime = collected)
precip_15_clean <- read_csv(here("intermediate_data", "precip_15_clean.csv")) 
precip_6h_clean <- read_csv(here("intermediate_data", "precip_6h_clean.csv")) 
precip_12h_clean <- read_csv(here("intermediate_data", "precip_12h_clean.csv")) 
precip_daily_clean <- read_csv(here("intermediate_data", "precip_daily_clean.csv")) 
se_clean <- read_csv(here("intermediate_data", "se_clean.csv"))

Set constant values

#set constants 
min_year = 2017
max_year = 2021

Create sub-directories if necessary

output_dir <- file.path(here("intermediate_data"))

if (!dir.exists(output_dir)){
dir.create(output_dir)
} else {
    print("Directory already exists!")
}
## [1] "Directory already exists!"
output_dir <- file.path(here("figures"))

if (!dir.exists(output_dir)){
dir.create(output_dir)
} else {
    print("Directory already exists!")
}
## [1] "Directory already exists!"

Streamflow compared to cumulative precipitation and manual checkpoints

Stream height (from stripchart data) is plotted as a blue line. Cumulative precipitation is plotted as grey bars. Stream height (from manual checkpoints) is plotted as black dots.

Cumulative Precipitation Every 6 Hours

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_6h_clean$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_6h <- ggplot() + 
  #precipitation data 
  geom_col(data = precip_6h_clean, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = I("grey")),
            size = 0.25) + 
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.4) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = paste0("S2 Bog Streamflow and Cumulative \n Precipitation Every 6 Hours (", min_year, "-", max_year, ")"),
       subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) +
  scale_y_continuous(
    #features of the first axis
    name = "Stream Height (ft)",
    #add a second axis and specify its features
    ##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Precipitation / 10 (in)")
  )

#static plot
#p_6h

#save the plot in the figures folder 
ggsave(filename = "streamflow_precip_6h_mc.png",
       plot = p_6h,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot 
ggplotly(p_6h)

Cumulative Precipitation Every 12 Hours

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_12h_clean$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_12h <- ggplot() + 
  #precipitation data 
  geom_col(data = precip_12h_clean, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = I("grey")),
            size = 0.25) + 
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.4) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = paste0("S2 Bog Streamflow and Cumulative \n Precipitation Every 12 Hours (", min_year, "-", max_year, ")"),
       subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) +
  scale_y_continuous(
    #features of the first axis
    name = "Stream Height (ft)",
    #add a second axis and specify its features
    ##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
    ###for example, max value of precipitation / max value of streamflow = 0.533...
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Precipitation / 10 (in)")
  )

#static plot
#p_12h

#save the plot in the figures folder 
ggsave(filename = "streamflow_precip_12h_mc.png",
       plot = p_12h,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot 
ggplotly(p_12h)

Cumulative Daily Precipitation

#format precipitation data for a smooth joining process
precip_daily_join <- precip_daily_clean %>% 
  pivot_wider(
    names_from = "precip_type",
    values_from = "precip_in"
  ) %>% 
  subset(year >= min_year & year <= max_year) %>% 
  rename(precip_10_in = precip_10) %>% 
  mutate(datetime = format(as.POSIXct(date, format = '%Y-%m-%d', 
                                  tz = "GMT"),
                           format = '%Y-%m-%d %H:%M:%S'),
         datetime = as.POSIXct(datetime))

#join streamflow and daily precipitation data 
streamflow_precip_daily <- full_join(x = all_streamflow, 
                                     y = precip_daily_join, 
                                     by = "date") %>% 
  select(-precip_in) %>% 
  subset(year >= min_year & year <= max_year)

#calculate the ratio of the 2 y-axes to plot the variables together
trans_value <- max(precip_daily_join$precip_10_in, na.rm = TRUE) / max(all_streamflow$stream_height_ft, na.rm = TRUE)

#plot 
p_daily <- ggplot() + 
  #precipitation data 
  geom_col(data = precip_daily_join, 
            aes(x = datetime, 
                y = precip_10_in,
                colour = I("grey")),
            size = 0.25) + 
  #streamflow data 
  geom_line(data = all_streamflow,
             aes(x = datetime, 
                 y = stream_height_ft),
            color = "blue",
            size = 0.4) + 
  #manual check points
  geom_point(data = mc_clean, 
             aes(x = datetime, y = stripchart_stage),
             size = 0.5) + 
  theme_classic() + 
  labs(x = "Time", 
       title = paste0("S2 Bog Streamflow and Cumulative \n Daily Precipitation (", min_year, "-", max_year, ")"),
       subtitle = "Streamflow is plotted in blue. \n Precipitation is plotted in grey. \n Manual streamflow checks are plotted as black points.") + 
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5, 
                                     size = 7)) +
  scale_y_continuous(
    #features of the first axis
    name = "Stream Height (ft)",
    #add a second axis and specify its features
    ##determine the ratio between the max value of Axis 1 vs the max value of Axis 2 to decide what number to put in the trans argument
    ###for example, max value of precipitation / max value of streamflow = 0.533...
    sec.axis = sec_axis(trans = ~.*trans_value, name = "Cumulative Daily Precipitation / 10 (in)")
  )

#static plot
#p_daily

#save the plot in the figures folder 
ggsave(filename = "streamflow_precip_daily_mc.png",
       plot = p_daily,
       path = "figures/",
       width = 8,
       height = 4,
       units = c("in"),
       dpi = 300)

#interactive plot 
ggplotly(p_daily)

Note that this file is being knit as index.html into the precip_comparisons repository in order to update the GitHub pages website, where the plots can be viewed online.